package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.text.InputType;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.MonthAttendanceListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseMonthTimeDialogFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.MonthAttendanceOriginal;
import com.isunland.managesystem.utils.MyDateUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * 对象功能: 月考勤数据自查
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2016/5/9 13:34
 */
@SuppressWarnings("RestrictedApi") public class MonthAttendanceListFragment
    extends BaseListFragment {

  private static int REQUIRE_DATA = 0;

  private EditText etMonth;
  private CurrentUser mUser;
  private MonthAttendanceListAdapter mAdapter;
  @SuppressWarnings("deprecation") private String month =
      MyDateUtil.dateToString(MyDateUtil.getBeforeOneMonth(), MyDateUtil.TYPE_DATE4);

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    setBack(true);
    setTitleCustom(R.string.monthAttendance);
    mUser = CurrentUser.newInstance(getActivity());
  }

  @Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    View header =
        getLayoutInflater(savedInstanceState).inflate(R.layout.header_search_project, null, false);
    TextView tvUserName = (TextView) header.findViewById(R.id.tv_search_date);
    tvUserName.setInputType(InputType.TYPE_NULL);
    //tvUserName.setTextIsSelectable(true);
    tvUserName.setEnabled(false);
    etMonth = (EditText) header.findViewById(R.id.et_search_project);
    etMonth.setFocusable(false);//不可编辑
    etMonth.setClickable(true);//可以点击
    tvUserName.setText(mUser.getRealName());
    etMonth.setText(month);
    etMonth.setHint("--请选择时间--");
    etMonth.setOnClickListener(new View.OnClickListener() {

      @Override public void onClick(View v) {
        DialogFragment dialog = new BaseMonthTimeDialogFragment();
        FragmentManager fm = getActivity().getSupportFragmentManager();
        dialog.setTargetFragment(MonthAttendanceListFragment.this, REQUIRE_DATA);
        dialog.show(fm, "");
      }
    });
    mListview.addHeaderView(header);
  }

  //@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  //  inflater.inflate(R.menu.menu_confirm, menu);
  //  super.onCreateOptionsMenu(menu, inflater);
  //}

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        return true;
      //case R.id.menu_item_confirm:
      //  month = etMonth.getText().toString();
      //  volleyPost();
      //  return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public String getUrl() {
    return ApiConst.URL_MONTH_ATTENDANCE;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("memberCode", mUser.getMemberCode());
    params.put("attendTime", month);
    params.put("staffId", mUser.getJobNumber());
    params.put("style", "'tree'");
    params.put("type", "mobile");
    return params;
  }

  @Override public void resolveResponse(String json) {
    MonthAttendanceOriginal original = new Gson().fromJson(json, MonthAttendanceOriginal.class);
    ArrayList<MonthAttendanceOriginal.MonthAttendanceContent> list = original.getRows();
    setAdapter(list);
  }

  private void setAdapter(ArrayList<MonthAttendanceOriginal.MonthAttendanceContent> list) {
    if (list == null) {
      list = new ArrayList<>();
    }
    if (mAdapter == null) {
      mAdapter = new MonthAttendanceListAdapter(getActivity(), list);
      setListAdapter(mAdapter);
      return;
    }
    mAdapter.clear();
    mAdapter.addAll(list);
    ((MonthAttendanceListAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    MonthAttendanceOriginal.MonthAttendanceContent content = mAdapter.getItem(position - 2);
    //if (content.getDataType() != null && "T".equalsIgnoreCase(content.getDataType())) {
    //  //实时结算
    //  Intent i = new Intent(getActivity(), MonthAttendanceRealtimeDetailActivity.class);
    //  i.putExtra(MonthAttendanceRealtimeDetailFragment.EXTRA_VALUE, content);
    //  startActivity(i);
    //} else {
    //  //月底结算
    //  Intent i = new Intent(getActivity(), MonthAttendanceDetailActivity.class);
    //  i.putExtra(MonthAttendanceDetailFragment.EXTRA_VALUE, content);
    //  startActivity(i);
    //}

    Intent i = new Intent(getActivity(), MonthAttendanceActivity.class);
    i.putExtra(MonthAttendanceFragment.EXTRA_ID, content != null ? content.getId() : null);
    startActivity(i);

    super.onListItemClick(l, v, position, id);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUIRE_DATA) {
      Date date = (Date) data.getSerializableExtra(BaseMonthTimeDialogFragment.EXTRA_DATE);
      //noinspection deprecation
      month = MyDateUtil.dateToString(date, MyDateUtil.TYPE_DATE4);
      etMonth.setText(month);
      volleyPost();

    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  //开始是否加载和显示空视图
  //    @Override
  //    protected void isPostStart(boolean isPostStart, boolean isShowEmpty) {
  //        super.isPostStart(false, false);
  //    }
}
